package com.vip.jwt.model.wx.service.impl;

import cn.hutool.json.JSONArray;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.vip.jwt.common.utils.xcx.wx.WXAuthUtil;
import com.vip.jwt.common.utils.xcx.wx.WXUserInfoOA;
import com.vip.jwt.model.wx.entity.Wxuser;
import com.vip.jwt.model.wx.mapper.WxuserMapper;
import com.vip.jwt.model.wx.service.WxuserService;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @description: 微信用户列表 逻辑实现层
 * @author: yw
 * @time: 2021-09-03 11:14:27
 * @version: 1.0.0
 */
@Service
public class WxuserServiceImpl implements WxuserService {

    @Autowired
    @SuppressWarnings("all")
    private WxuserMapper wxuserMapper;

    /**
     * 同步微信用户信息
     */
    @Override
    public void sysnWxUserList() {
        List<WXUserInfoOA> wxUserInfoOAS = WXAuthUtil.syncUserInfo();
        for (WXUserInfoOA wxUserInfoOA : wxUserInfoOAS) {
            Wxuser wxuser = new Wxuser();
            wxuser.setOpenid(wxUserInfoOA.getOpenid());
//            wxuser.setCity(wxUserInfoOA.getCity());
//            wxuser.setCountry(wxUserInfoOA.getCountry());
//            wxuser.setProvince(wxUserInfoOA.getProvince());
            wxuser.setRemark(wxUserInfoOA.getRemark());
            wxuser.setHeadimgurl(wxUserInfoOA.getHeadimgurl());
            wxuser.setNickname(wxUserInfoOA.getNickname());
            wxuser.setSubscribeTime(Integer.valueOf(wxUserInfoOA.getSubscribe_time()));
            wxuserMapper.insert(wxuser);
        }

    }

    /**
     * @Description 列表查询
     * @Author yw
     * @Date 2021-09-03 11:14:27
     * @Param
     * @return
     **/
    @Override
    public IPage<Wxuser> queryPage(int current, int size) {
        QueryWrapper<Wxuser> queryWrapper = new QueryWrapper<>();
        Page<Wxuser> page = new Page<>(current, size);
        return wxuserMapper.selectPage(page, queryWrapper);
    }

    /**
     * @Description 根据id获取信息
     * @Author yw
     * @Date 2021-09-03 11:14:27
     * @Param
     * @return
     **/
    @Override
    public Wxuser getById(String openid) {
        return wxuserMapper.selectById(openid);
    }

    /**
     * @Description 保存
     * @Author yw
     * @Date 2021-09-03 11:14:27
     * @Param
     * @return
     **/
    @Override
    public Boolean save(Wxuser wxuser) {
        return wxuserMapper.insert(wxuser) > 0;
    }

    /**
     * @Description 修改
     * @Author yw
     * @Date 2021-09-03 11:14:27
     * @Param
     * @return
     **/
    @Override
    public Boolean updateById(Wxuser wxuser) {
        return wxuserMapper.updateById(wxuser) > 0;
    }

    /**
     * @Description 删除
     * @Author yw
     * @Date 2021-09-03 11:14:27
     * @Param
     * @return
     **/
    @Override
    public Boolean removeById(String openid) {
        return wxuserMapper.deleteById(openid) > 0;
    }
}